<?php
/**
 * admin_role
 * 管理员角色关联表
 * @author Administrator
 * @version
 */
class Model_Tables_AdminRole extends Zend_Db_Table_Abstract {
    /**
     * The default table name
     */
    protected $_name = 'admin_role';
    
    public function add( $params )
    {
        $data = array(
		'userid' => $params['userid'],
		'roleid' => $params['roleid'],
        );
        return $this->insert($data);
    }
    public function modify( $params, $id )
    {
        $data = array(
		'userid' => $params['userid'],
		'roleid' => $params['roleid'],
        );
        $where = 'id = '.$id ;
        return $this->update($data , $where );
    }
    public function modifyAjax( $id, $field, $value )
    {
    	$data = array($field => $value);
        $where = 'id = '.$id;
        return $this->update($data , $where );
    }
    public function modifyAjaxRevers( $field ,$pri)
    {
    	$db = $this->getAdapter();
    	$sql = "update admin_role set  $field =not  $field  where id=' $pri'";
		return $db->query($sql);
    }
    public function del( $id )
    {
     	$where = 'id = '.$id;
        return $this->delete($where);
    }
    public function getInfo($id)
    {
        $row = $this->fetchRow('id = ' . $id);
        if (!$row) {
        	return null;
        }
        else
        {
        	return $row->toArray();
        }
    }
    public function getCount($s = "")
    { 
        $select = $this->select()->from($this->_name,'count(*) as num');
    	if($s!="")  
    	{
    		$db = $this->getAdapter();
			$where = $db->quoteInto("id=?", $s);
    		$select = $select->where($where);
    	}
        $result = $this->fetchRow($select)->num;
        return $result;
    }
    public function getList($page, $numPerPage = 10,$s= "")
    {
    	$page = (1>$page) ? 1 : $page;
    	$offset = ($page-1)*$numPerPage;
        $orderby = array('id');
        if($s!="")     
		{
			$db = $this->getAdapter();
			$where = $db->quoteInto("id= ?", $s);
		}  
        $result = $this->fetchAll($where,$orderby, $numPerPage, $offset )->toArray();
        return $result;
    }
    /*
     * 获取管理员角色,返回字符串格式
     */
    public function getAdminRole($userid)
    {
    	$result = "";
    	$userid = intval($userid);
   		$db = $this->getAdapter();
    	$selectsql = "select GROUP_CONCAT(roleid) as v from admin_role where  userid ='$userid'";
    	$result = $db->query($selectsql)->fetchAll();
    	if (!empty($result))
	    {
	      	$result = $result[0]['v'];	            
	    }
    	return $result;
    }
}
 
